﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Phidgets;
using Phidgets.Events;

namespace EntranceApp
{
    public partial class Form1 : Form
    {
        Entrance entrance = new Entrance();
        DBhelper dbhelp = new DBhelper();
        Client myClient;
        bool isPresed = false;
        string tag;
        RFID rfid =  new RFID();
        
        public Form1()
        {
            

            InitializeComponent();
            
            myClient = new Client("","","",0,DateTime.Now);
            try
            {
                

                //initialize our Phidgets RFID reader and hook the event handlers

                rfid.Detach += new DetachEventHandler(rfid_Detach);
                rfid.Attach += new AttachEventHandler(rfid_Attach);
                rfid.open();
                rfid.Error += new ErrorEventHandler(rfid_Error);

                rfid.Tag += new TagEventHandler(rfid_Tag);
                rfid.TagLost += new TagEventHandler(rfid_TagLost);
                                                          
            }
            catch (PhidgetException ex)
            {
                MessageBox.Show(ex.Description);
            }
        }

        //attach event handler...display the serial number of the attached RFID phidget
        public void rfid_Attach(object sender, AttachEventArgs e)
        {
            l_reader.Text = "Reader number: " + e.Device.SerialNumber.ToString() + " is attached!";
        }

        //detach event handler...display the serial number of the detached RFID phidget
        public void rfid_Detach(object sender, DetachEventArgs e)
        {
            l_reader.Text = "Reader number: " + e.Device.SerialNumber.ToString() + " is detached!";
        }

        //Error event handler...display the error description string
        public void rfid_Error(object sender, ErrorEventArgs e)
        {
            MessageBox.Show(e.Description);
        }

        //Print the tag code of the scanned tag
        public void rfid_Tag(object sender, TagEventArgs e)
        {
            //l_scanlost.Text = "scanned";
            tag = e.Tag;
            l_scanlost.ForeColor = Color.Green;

            myClient = entrance.SearchClient(e.Tag);
            l_name.Text = myClient.First_name+"  "+myClient.Last_name;
            l_rfid.Text = myClient.Account_id;
            l_balance.Text = myClient.Balance.ToString();
            if (System.DateTime.Now.Year - 18 >= myClient.Date_birth.Year)
            {
                pictureBox1.Image = imageList1.Images[1];
            }
            else if (System.DateTime.Now.Year - 18 < myClient.Date_birth.Year)
            {
                pictureBox1.Image = imageList1.Images[0];
                //MessageBox.Show("You are under age");
            }
            

            if (entrance.CheckInEvent(myClient.Account_id)==1)
            {
                entrance.InEvent(myClient.Account_id,0);
            }
            else if (entrance.CheckInEvent(myClient.Account_id)==0)
            {
                entrance.InEvent(myClient.Account_id,1);
            }
            

        }

        //print the tag code for the tag that was just lost
        public void rfid_TagLost(object sender, TagEventArgs e)
        {
            //l_scanlost.Text = "lost";
           // l_rfid.Text = "-----";
            //l_name.Text = "-----";
            l_scanlost.ForeColor = Color.Red;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!isPresed)
            {
                ButtonStarted();
            }
            else if (isPresed)
            {
                ButtonStop();
            }
        }
        public void ButtonStarted()
        {
            //rfid = new RFID(); //Declare an RFID object
            isPresed = true;
            //MessageBox.Show("Started");
            rfid.Attach += new AttachEventHandler(rfid_Attach);
            rfid.open();


            //Wait for a Phidget RFID to be attached before doing anything with 
            //the object

            rfid.waitForAttachment();

            //turn on the antenna and the led to show everything is working
            rfid.Antenna = true;
            rfid.LED = true;
            button1.Text = "Stop RFID";
        }
        public void ButtonStop()
        {
            isPresed = false;
            //MessageBox.Show("Stoped");
           // rfid.Detach += new DetachEventHandler(rfid_Detach);
            button1.Text = "Start RFID";
            //close the phidget and dispose of the object
            rfid.close();
          

        }

        

        private void button2_Click_1(object sender, EventArgs e)
        {
            double amountOfMoney = Convert.ToDouble(textBox1.Text);
            Client clien = entrance.SearchClient(tag);
            entrance.RechargeMoneyBalance(amountOfMoney, clien.Account_id);
            //MessageBox.Show("Monei in");
        }

        
       
    }
}
